package org.ihelpuoo.parser;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

import org.ihelpuoo.io.StringBufferOutputStream;
import org.ihelpuoo.lang.RenderException;

public abstract class AbstractTextRender implements TextRender
{
	public void render2(OutputStream ops) throws RenderException
	{
		try
		{
			Writer bw = new OutputStreamWriter(ops);
			render2(bw);
			bw.close();
		}
		catch (IOException e)
		{
			throw new RenderException(e);
		}
	}

	@Override
	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		StringBufferOutputStream sos = new StringBufferOutputStream(sb);
		try
		{
			this.render2(sos);
			sos.close();
			return sb.toString();
		}
		catch (RenderException e)
		{
			throw new RuntimeException(e);
		}
		catch (IOException e)
		{
			throw new RuntimeException(e);
		}

	}
}
